package com.qianfeng.openplatform.geteway.rabbit;

import com.qianfeng.openplatform.commons.field.util.AppTowCacheUtil;
import com.qianfeng.openplatform.commons.field.util.RabbitAdviceBean;
import com.qianfeng.openplatform.geteway.client.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import javax.xml.ws.Action;
import java.util.Map;

/**
 * @author 江江
 * @Date 2020/7/28 20:07
 */
@Component
@EnableBinding(ReciveRabbitMessage.class)
public class ReciveRoutingListener {
    @Autowired
    private CacheClient cacheClient;

    @StreamListener("routing_change-ext")
    public void getMessage(RabbitAdviceBean bean){
        String key = bean.getMessage();
        switch (bean.getType()){

            case UPDATE:
                System.out.println("ReciveRoutingListener=========>"+bean.getMessage());
                //这里面更新缓存
                //获取到缓存中更新的key
                Map<Object, Object> map = null;
                try {
                    map = cacheClient.hGetAll(key);
                    AppTowCacheUtil.cacheRoutingMap.put(key,map);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case DELETE:
                //删除二级缓存中是否存在这个key及对应的值,先判断,如果不存在这个key则不需要操作,存在在删除
                if (AppTowCacheUtil.cacheRoutingMap.containsKey(key)){
                    AppTowCacheUtil.cacheRoutingMap.remove(key);
                }
                break;
        }
    }
}
